package ar.uba.dc.cmovil.tripplanner.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;

import java.util.Collection;

import ar.uba.dc.cmovil.tripplanner.R;
import ar.uba.dc.cmovil.tripplanner.domain.Trip;
import ar.uba.dc.cmovil.tripplanner.domain.TripsManager;
import ar.uba.dc.cmovil.tripplanner.utils.TextUtils;
import ar.uba.dc.cmovil.tripplanner.view.TripsView;
import ar.uba.dc.cmovil.tripplanner.widget.TextAndDateWidget;
import ar.uba.dc.cmovil.tripplanner.widget.TripItemWidget;

public class ViewTripsActivity extends Activity implements TripItemWidget.TripItemEventsHandler {

    private TripsView view;
    private TripsManager tripsManager;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StrictMode.ThreadPolicy
                policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        tripsManager = TripsManager.getInstance();

        view = new TripsView(this);

        tripsManager.setContext(this.getApplicationContext());
        Collection<Trip> trips = tripsManager.loadTrips();

        for (Trip trip : trips) {
            addTripToView(trip);
        }

        view.newTripButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newTripButtonClicked();
            }
        });

        setTitle("Trip Planner");
    }

    private void newTripButtonClicked() {
        final TextAndDateWidget widget = new TextAndDateWidget(this, "Nombre del viaje");

        new AlertDialog.Builder(this)
                .setTitle("Nuevo viaje")
                .setView(widget)
                .setPositiveButton("Guardar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        if (!TextUtils.isEmpty(widget.getText())) {
                            Trip trip = new Trip(widget.getText(), widget.getDate());

                            addTrip(trip);
                            addTripToView(trip);
                        }
                    }
                })
                .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                    }
                })
                .show();
    }

    private void addTrip(Trip trip) {
        this.tripsManager.add(trip);
        this.tripsManager.saveTrips();
    }

    private void addTripToView(Trip trip) {
        view.addTrip(trip);
    }

    @Override
    public void onTripClicked(Trip trip) {
        Intent places = new Intent(this, PlacesActivity.class);
        places.putExtra(PlacesActivity.SELECTED_TRIP_ID, trip.getId().toString());
        startActivity(places);
    }

    @Override
    public void onTripDeleteClicked(final Trip trip) {
        final TextView tripName = new TextView(this);
        tripName.setText(trip.getName());

        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(R.string.delete)
                .setMessage(R.string.confirm_delete_trip)
                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        removeTrip(trip);
                    }

                })
                .setNegativeButton(R.string.no, null)
                .show();

    }

    private void removeTrip(Trip trip) {
        this.view.removeTrip(trip);
        this.tripsManager.remove(trip);
        this.tripsManager.saveTrips();
    }
}
